package co.example.michael.tools;

import android.net.Uri;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.logging.Handler;

import co.example.michael.bigpictureloading.FinalConstant;

/**
 * Created by Michael on 2015/3/20.
 */
public class InternetTools {

    //网络异步请求对象
    private AsyncHttpClient httpClient;

    public static InternetTools internetTools;

    public InternetTools(){
        httpClient = new AsyncHttpClient();
    }

    /**
     * 单例模式返回对象
     * @return
     */
    public static InternetTools getInstance(){
        if (internetTools==null){
            internetTools = new InternetTools();
        }
        return internetTools;
    }

    /**
     * 在指定接口获取JSON数据
     * @param strAllUrl 这个就是get请求的url,包括各个url参数的键值
     * Get 请求
     */
    public  String jsonData;
    public String getJsonData(String strAllUrl){
        jsonData = "";
        httpClient.get(strAllUrl,new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {

                jsonData = new String(bytes);

                Log.e("InternetTools-从服返回的数据：",jsonData);
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                throwable.printStackTrace();
//                jsonData = FinalConstant.REQUEST_ERROR;
            }
        });

        return jsonData;
    }


}
